package servletDemo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(urlPatterns = "/req")
public class requestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*request 获取请求行数据*/
//        获取请求方式
        String method = req.getMethod();
        System.out.println("request method: " + method);
//        获取协议版本号
        String protocol = req.getProtocol();
        System.out.println("protocol: " + protocol);
//        获取请求方的ip地址
        String address = req.getRemoteAddr();
        System.out.println("address: " + address);
//        获取访问的servlet的虚拟路径
        String servlet_path = req.getServletPath();
        System.out.println("servlet_path: " + servlet_path);
//        获取虚拟目录（项目访问路径）
        String context_path = req.getContextPath();
        System.out.println("context_path: " + context_path);
//        获取URL（统一资源定位符）
        StringBuffer requestUrl = req.getRequestURL();
        System.out.println("requestUrl: " + requestUrl);
//        获取URI（统一资源标识符）
        String requestUri = req.getRequestURI();
        System.out.println("requestUri: " + requestUri);
//        获取请求参数
        String queryString = req.getQueryString();
        System.out.println("queryString: " + queryString);

        /*request 获取请求头数据*/
//        根据请求头中的名称获取相应的值
        String host = req.getHeader("Host");
        System.out.println("host: " + host);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取请求参数数据*/
//        获取所有参数Map集合
        Map<String, String[]> reqArgs = req.getParameterMap();

//        根据名称获取数参数值  数组
        String[] values = req.getParameterValues("username");
//        根据名称获取数参数值  单个值
        for (String value : values) {
            System.out.print("args: " + value);
            System.out.println();
        }

        String username = req.getParameter("username");
        System.out.println("usrname: " + username);
    }
}
